package com.njitt.bysj.srevice;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.njitt.bysj.base.BaseService;
import com.njitt.bysj.bean.DeptBean;
import com.njitt.bysj.bean.EmpDeptBean;
import com.njitt.bysj.mapper.DeptBeanMapper;
import com.njitt.bysj.mapper.EmpDeptBeanMapper;
import com.njitt.bysj.query.DeptQuery;
import com.njitt.bysj.utils.AssertUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: wyj
 * @time: 2022/3/19 11:45
 */
@Service
public class DeptService extends BaseService<DeptBean, Integer> {

    @Resource
    private DeptBeanMapper deptMapper;

    @Resource
    private EmpDeptBeanMapper empDeptBeanMapper;

    public Map<String, Object> selectDept(DeptQuery deptQuery) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(deptQuery.getPage(), deptQuery.getLimit());
        PageInfo<DeptBean> pageInfo = new PageInfo<>(deptMapper.selectByParams(deptQuery));
        map.put("code", 0);
        map.put("msg", "success");
        map.put("data", pageInfo.getList());
        map.put("count", pageInfo.getTotal());
        return map;
    }

    public void deleteDept(Integer id) {
        AssertUtil.isTrue(id == null, "删除记录不能为空");
        DeptBean deptBean = deptMapper.selectByPrimaryKey(id);
        AssertUtil.isTrue(deptBean == null, "待删除记录不存在");
        Integer integer = deptMapper.deleteByPrimaryKey(id);
        AssertUtil.isTrue(integer != 1, "删除失败");

    }

    @SuppressWarnings("AlibabaTransactionMustHaveRollback")
    @Transactional(propagation = Propagation.REQUIRED)
    public void addDept(DeptBean deptBean) {
        AssertUtil.isTrue(StringUtils.isBlank(deptBean.getDeptName()), "部门名称不能为空");
        Integer integer = deptMapper.insertSelective(deptBean);
        AssertUtil.isTrue(integer == null, "添加失败");
    }

    @SuppressWarnings("AlibabaTransactionMustHaveRollback")
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateDept(DeptBean deptBean) {
        AssertUtil.isTrue(deptBean.getId() == null, "编号为空");
        Integer integer = deptMapper.updateByPrimaryKeySelective(deptBean);
        AssertUtil.isTrue(integer == null, "添加失败");
    }

    public List<Map<String, Object>> queryAllDeptToSelected() {
        return deptMapper.queryAllDeptToSelected();
    }


    public Map<String, Object> empDept(DeptQuery deptQuery) {
        PageHelper.startPage(deptQuery.getPage(), deptQuery.getLimit());
        PageInfo<EmpDeptBean> info = new PageInfo<>(empDeptBeanMapper.selectByParams(deptQuery));
        Map<String, Object> map = new HashMap<>();
        map.put("data", info.getList());
        map.put("count", info.getTotal());
        map.put("msg", "success");
        map.put("code", 0);
        return map;

    }

    public EmpDeptBean selectEmpDept(Integer deptId) {
        System.out.println(deptId);
        EmpDeptBean empDeptBean = empDeptBeanMapper.selectByPrimaryKey(deptId);
        return empDeptBean;
    }
}
